package com.yj.admin.config;

import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;
import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

@Component
public class LoggerNotifier extends AbstractStatusChangeNotifier {

    private Logger logger = LoggerFactory.getLogger(getClass());

    public LoggerNotifier(InstanceRepository repository) {
        super(repository);
    }

    /**
     * 自定义监听实例事件
     */
    @Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        return Mono.fromRunnable(() -> {
            if (event instanceof InstanceStatusChangedEvent) {
                logger.info("服务 {} (实例ID：{}) {} to {}", instance.getRegistration().getName(), instance.getId(),
                        event.getType(), ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus());
            } else {
                logger.info("服务 {} (实例ID：{}) {}", instance.getRegistration().getName(), instance.getId(),
                        event.getType());
            }
        });
    }

}